package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.ResignationApplication;
import com.amsp.oa.oademo.mapper.ResignationApplicationMapper;
import com.amsp.oa.oademo.service.ResignationApplicationService;
import com.amsp.oa.oademo.vo.ResignationApplicationVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 职员/员工离职申请表Service实现类
 */
@Service
public class ResignationApplicationServiceImpl extends ServiceImpl<ResignationApplicationMapper, ResignationApplication>
        implements ResignationApplicationService {

    @Override
    public Page<ResignationApplicationVO> getList(Page<ResignationApplication> page, String name, String department) {
        // 构建查询条件
        LambdaQueryWrapper<ResignationApplication> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(ResignationApplication::getName, name);
        }
        if (StringUtils.hasText(department)) {
            wrapper.like(ResignationApplication::getDepartment, department);
        }
        wrapper.orderByDesc(ResignationApplication::getCreateTime);

        // 执行分页查询
        Page<ResignationApplication> entityPage = page(page, wrapper);

        // 转换为VO对象
        Page<ResignationApplicationVO> voPage = new Page<>();
        BeanUtils.copyProperties(entityPage, voPage, "records");

        List<ResignationApplicationVO> voList = entityPage.getRecords().stream().map(entity -> {
            ResignationApplicationVO vo = new ResignationApplicationVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());

        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public ResignationApplicationVO getDetail(Long id) {
        ResignationApplication entity = getById(id);
        if (entity == null) {
            return null;
        }

        ResignationApplicationVO vo = new ResignationApplicationVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(ResignationApplicationVO vo) {
        ResignationApplication entity = new ResignationApplication();
        BeanUtils.copyProperties(vo, entity);
        return saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<ResignationApplicationVO> voList) {
        if (voList == null || voList.isEmpty()) {
            return false;
        }

        List<ResignationApplication> entityList = voList.stream().map(vo -> {
            ResignationApplication entity = new ResignationApplication();
            BeanUtils.copyProperties(vo, entity);
            return entity;
        }).collect(Collectors.toList());

        return saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return removeByIds(ids);
    }
}